<template>
	<view class="content b-t"  v-if="isShow">
		
		<view class="list b-b" v-for="(item, index) in addressList" :key="index" >
			<radio :value="item.value" :checked="item.is_default==1" style="margin-right:10px;" @click="checkAddress(item)" />
			<view class="wrapper" @click="checkAddress(item)" >
				<view class="address-box">
					<text class="name">{{item.name}}</text>
					<text class="mobile">{{item.mobile}}</text>
				</view>
				<view class="u-box">
					<text class="address">{{item.address}} {{item.area}}</text>
				</view>
			</view>
			<text class="yticon icon-bianji iconfont" @click.stop="delAddress(item)">&#xe60f;</text>
			<text class="yticon icon-bianji iconfont" @click.stop="addAddress('edit', item)">&#xe621;</text>
		</view>
		
		<!-- 空白页 -->
		<view class="empty-data" v-if="!addressList.length">
			<view class="iconfont">&#xe869;</view>您还有没有地址数据
		</view>
		
		<button class="add-btn" @click="addAddress('add')">新增地址</button>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				isShow:false,
				source: 0,
				addressList:[]
			}
		},
		onLoad(option){
			this.source = option.source;
			this.getAddressData();
		},
		methods: {
			//获取地址
			getAddressData(item){
				this.$request('/app/v1/Address/getAddressData').then((res)=>{
					this.isShow = true;
					this.addressList = res.data || [];
				});
			},
			//选择地址
			checkAddress(item){
				this.addressList.forEach(h=>{
					h.is_default=0;
				});
				item.is_default = 1;
				this.$request("/app/v1/Address/editAddress",item).then((res)=>{
					if(this.$util.prePage()) this.$util.prePage().addressData = item;
					this.$util.msg("已变更默认地址");
					uni.navigateBack()
				});
			},
			addAddress(type, item){
				uni.navigateTo({
					url: `/pages/address/addressManage?type=${type}&data=${JSON.stringify(item)}`
				})
			},
			// 删除地址
			delAddress(item){
				var that = this;
				uni.showModal({
					icon: 'none',
					position:"top",
					title:"提示",
					showCancel:true,
					content:"确定删除地址？",
					success:function(e){
						if(e.confirm){
							that.$request("/app/v1/Address/delAddress",{
								address_ids:item.address_id,
							}).then((res)=>{
								that.addressList = res.data || [];
							});
						} 
					}
				});
			},
			//添加或修改成功之后回调
			refreshList(data, type){
				//添加或修改后事件，这里直接在最前面添加了一条数据，实际应用中直接刷新地址列表即可
				this.getAddressData();
			}
		}
	}
</script>

<style lang='scss'>
	page{
		padding-bottom: 120upx;
	}
	.content{
		position: relative;
	}
	.list{
		display: flex;
		align-items: center;
		padding: 20upx 30upx;;
		background: #fff;
		position: relative;
		border-bottom: 1px solid #f8f8f8;
	}
	.wrapper{
		display: flex;
		flex-direction: column;
		flex: 1;
	}
	.address-box{
		display: flex;
		align-items: center;
		.tag{
			font-size: 24upx;
			color: $base-color;
			margin-right: 10upx;
			background: #fffafb;
			border: 1px solid #ffb4c7;
			border-radius: 4upx;
			padding: 4upx 10upx;
			line-height: 1;
		}
		.name{
			margin-right: 30upx;
		}
		.address{
			font-size: 30upx;
			color: $font-color-dark;
		}
	}
	.u-box{
		font-size: 25upx;
		color: $font-color-light;
		margin-top: 5upx;
	}
	.icon-bianji{
		display: flex;
		align-items: center;
		height: 80upx;
		font-size: 50upx;
		color: $font-color-light;
		padding-left: 30upx;
	}
	
	.add-btn{
		position: fixed;
		left: 30upx;
		right: 30upx;
		bottom: 16upx;
		z-index: 95;
		display: flex;
		align-items: center;
		justify-content: center;
		width: 690upx;
		height: 80upx;
		font-size: 32upx;
		color: #fff;
		background-color: $base-color;
		border-radius: 10upx;
		box-shadow: 1px 2px 5px rgba(219, 63, 96, 0.4);		
	}
</style>
